package com.syni.mdd.yhd.common.all.config.common;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.config.common.groupTemplate.GroupSmsTemplate;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.entity.bo.common.MddDxshAppNewAdapterBo;
import com.syni.mdd.yhd.common.all.entity.permission.PermessionCodeUtils;
import com.syni.mdd.yhd.common.all.entity.permission.PermissionCode;
import com.syni.mdd.yhd.common.all.entity.push.PushTemplate;
import jodd.util.StringUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @className PermessionCodeUtils
 * @description 权限码工具类
 * @Author cfx
 * @DATE 2019/7/11 10:55
 * @VERSION 1.0
 **/
@Component
@Log4j2
public class CommonSetUtils {

    @Value("classpath:commonset.json")
    private Resource resource;

    public static List<String> mustVideo = new LinkedList<>();

    public static Map<Integer,GroupSmsTemplate> teteGroupBuyTemplates = new HashMap<>();

    @PostConstruct
    public void init(){
        String jsonCode = null;
        try {
            jsonCode = IOUtils.toString(new InputStreamReader(resource.getInputStream(),"UTF-8"));
            CommonSet commonSet = JSON.parseObject(jsonCode, CommonSet.class);
            if(commonSet != null){
                mustVideo = commonSet.getMustVideo();
                //初始化短信模板
                initTeteGroupBuyTemplates(commonSet);
                //初始化电享生活app的消息匹配
                initDxshappNewMacth(commonSet.getDxshAppNewMatch());
                //初始化权限值
                initPermissionCode(commonSet.getPermissionCodes());
                //初始化推送配置
                initPushNewAlterTempate(commonSet.getPushNewsSets());
            }
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }

    }

    private void initPushNewAlterTempate(List<PushTemplate> pushTemplates) {
        if(CollectionUtils.isNotEmpty(pushTemplates)){
            for(PushTemplate pushTemplate : pushTemplates) {
                if (StringUtil.isNotEmpty(pushTemplate.getCodeUp())) {
                    AuroraPushCode.pushTemplateImproveMap.put(pushTemplate.getCodeUp(), pushTemplate.getAlerts());
                } else {
                    AuroraPushCode.pushTemplateMap.put(pushTemplate.getCode(), pushTemplate);
                }
            }
        }
    }

    private void initPermissionCode(List<PermissionCode> permissionCodes) {
        if(CollectionUtils.isNotEmpty(permissionCodes)){
            for(PermissionCode permissionCode : permissionCodes){
                List<PermissionCode.CodeAttribute> codeAttributes = permissionCode.getCodeAttributes();
                List<Integer> codeList = new LinkedList<>();
                if(codeAttributes != null && codeAttributes.size() > 0) {
                    for(PermissionCode.CodeAttribute codeAttribute : codeAttributes){
                        codeList.add(codeAttribute.getCode());
                    }
                    permissionCode.setCodeList(codeList);
                }
                PermessionCodeUtils.permissionCodeMap.put(permissionCode.getRoleId(),permissionCode);
            }
        }
    }

    private void initDxshappNewMacth(List<MddDxshAppNewAdapterBo> mddDxshAppNewAdapterBos) {
        if(CollectionUtils.isNotEmpty(mddDxshAppNewAdapterBos)){
            for(MddDxshAppNewAdapterBo mddDxshAppNewAdapterBo : mddDxshAppNewAdapterBos){
                MddDxshAppNewAdapterBo.getMddDxshAppNewAdapterBoHashMap().put(mddDxshAppNewAdapterBo.getParentType() + "_" + mddDxshAppNewAdapterBo.getChildType(),mddDxshAppNewAdapterBo);
            }
        }
    }

    public void initTeteGroupBuyTemplates(CommonSet commonSet){
        List<GroupSmsTemplate> groupSmsTemplates = commonSet.getTeteGroupBuyTemplates();
        if(groupSmsTemplates != null && groupSmsTemplates.size() > 0){
            for(GroupSmsTemplate groupSmsTemplate : groupSmsTemplates){
                teteGroupBuyTemplates.put(groupSmsTemplate.getGroupId(),groupSmsTemplate);
            }
        }
    }

}
